package com.jz.community.moduleshopping.orderList.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.TextView;

import com.jz.community.basecomm.utils.ArrayUtil;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.orderList.adapter.SpecialCouponAdapter;
import com.jz.community.moduleshopping.orderList.bean.CouponReceiveBean;
import com.jz.community.moduleshopping.orderList.bean.SpecialCouponBean;
import com.jz.community.moduleshopping.orderList.task.CouponReceiveTask;

import java.util.ArrayList;
import java.util.List;

public class SpecialCouponDialog extends Dialog {


    private TextView tvSpecialCouponTitle;
    private RecyclerView rvSpecialCouponList;
    private Button btnSpecialCoupon;
    private TextView tvSpecialCouponExit;
    private SpecialCouponBean specialCouponBean;
    private SpecialCouponAdapter adapter;
    private Context context;

    public SpecialCouponDialog(@NonNull Context context) {
        super(context, R.style.Currency_Dialog);
        this.context = context;
    }


    public void setData(SpecialCouponBean specialCouponBean) {
        this.specialCouponBean = specialCouponBean;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.module_shopping_dialog_special_coupon);
        tvSpecialCouponTitle = findViewById(R.id.tv_special_coupon_title);
        rvSpecialCouponList = findViewById(R.id.rv_special_coupon_list);
        btnSpecialCoupon = findViewById(R.id.btn_special_coupon);
        tvSpecialCouponExit = findViewById(R.id.tv_special_coupon_exit);
        adapter = new SpecialCouponAdapter(R.layout.module_shopping_item_special_coupon, new ArrayList<>());
        rvSpecialCouponList.setLayoutManager(new LinearLayoutManager(context));
        rvSpecialCouponList.setAdapter(adapter);
        if (!Preconditions.isNullOrEmpty(specialCouponBean.get_embedded()) && !Preconditions.isNullOrEmpty(specialCouponBean.get_embedded().getContent())) {
            tvSpecialCouponTitle.setText("送您" + specialCouponBean.get_embedded().getContent().size() + "张优惠券");
            adapter.setNewData(specialCouponBean.get_embedded().getContent());
            btnSpecialCoupon.setOnClickListener(v -> {
                List<String> ids = new ArrayList<>();
                for (int i = 0; i < specialCouponBean.get_embedded().getContent().size(); i++) {
                    ids.add(specialCouponBean.get_embedded().getContent().get(i).getId());
                }
                couponReceive(ArrayUtil.listToString(ids));
            });
        }
        tvSpecialCouponExit.setOnClickListener(v -> dismiss());
    }


    private void couponReceive(String ids) {
        new CouponReceiveTask((Activity) context, o -> {
            CouponReceiveBean bean = (CouponReceiveBean) o;
            if (Preconditions.isNullOrEmpty(bean)) {
                WpToast.l(context, "手慢了没抢到");
                dismiss();
            } else {
                if (!Preconditions.isNullOrEmpty(bean.get_embedded()) && !Preconditions.isNullOrEmpty(bean.get_embedded().getContent())) {
                    if (bean.get_embedded().getContent().size() == 3) {
                        WpToast.l(context, "全部抢到了");
                    } else {
                        WpToast.l(context, "手慢了，只抢到" + bean.get_embedded().getContent().size() + "张");
                    }
                    dismiss();
                } else {
                    WpToast.l(context, "手慢了没抢到");
                    dismiss();
                }
            }
        }).execute(ids);
    }
}
